<template>
  <li>
    <div class="left_box">
      <div
        class="num"
        :class="song_data.idx <= 3 && is_rank ? 'changeRed' : ''"
        v-if="song_data.idx && !is_recommend"
      >
        {{ song_data.idx.toString().padStart(2, "0") }}
      </div>
      <div class="left_d1">
        {{ song_data.song_name }}
        <div class="left_d2">
          <img src="../assets/sq.png" v-if="song_data.song_exclusive" />{{
            song_data.song_artists | singer
          }}
          - {{ song_data.song_album }}
        </div>
      </div>
    </div>
    <div class="right_box" @click="play_song">
      <van-icon
        v-if="
          $store.state.playPause &&
            this.$store.state.song_info.id == this.song_data.id
        "
        name="pause-circle-o"
      />
      <van-icon v-else name="play-circle-o" />
    </div>
  </li>
</template>

<script>
import { check_music } from "../api/comment";
import { Toast } from "vant";
export default {
  props: {
    song_data: Object,
    //用于接受当前歌曲的歌单id集合
    id_list: Array,
    //idx 用于接收一个下标，表示当前歌曲在歌单内的位置
    //为了方便后续通过 +1 -1 来切换上下首
    idx: Number,
  },
  data() {
    return {
      is_rank: true,
      is_recommend: true,
    };
  },
  watch: {
    $route: {
      handler(val) {
        if (val.name == "Rank") {
          this.is_rank = true;
        } else {
          this.is_rank = false;
        }

        if (val.name == "Recommend") {
          this.is_recommend = true;
        } else {
          this.is_recommend = false;
        }
      },
      immediate: true,
    },
  },
  computed: {},
  methods: {
    async play_song() {
      //播放音乐前，先验证是否有版权，如果没有则使用Toast 弹窗提示;
      let check_res = await check_music(this.song_data.id);
      //如果有权限，则获取歌曲详情，放至vuex
      if (check_res.data.success) {
        this.$audio.playSong(this.song_data.id);

        // let song_detail = await get_song_detail(this.song_data.id);
        // let picUrl = song_detail.data.songs[0].al.picUrl;

        //播放音乐并且传递歌曲信息到仓库

        this.$store.dispatch("get_song_detail", this.song_data.id);

        // this.$store.commit("changeSong_info", {
        //   ...this.song_data,
        //   song_pic: picUrl,
        // });
        //将当前 歌曲所在列表的id集合存放到id_list
        this.$store.commit("set_play_list", this.id_list);
        this.$store.commit("changeState", true);

        this.$store.commit("change_idx", this.idx);
      } else {
        //弹窗提醒用户没有版权
        Toast(check_res.data.message);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
li {
  width: 100%;
  height: 50px;
  display: flex;
  justify-content: space-between;
  padding: 0 7px;
  overflow: hidden;
  border-bottom: 1px solid rgba($color: #5e5c5c, $alpha: 0.1);
  .left_box {
    width: 80%;
    align-items: center;
    display: flex;
    .num {
      font-size: 20px;
      padding: 5px 5px 5px 0;
      line-height: 40px;
    }
    .left_d1 {
      text-align: left;
      padding: 4px 0;
      font-size: 16px;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      .left_d2 {
        font-size: 12px;
        color: rgb(155, 152, 152);
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        img {
          margin-right: 5px;
          vertical-align: bottom;
        }
      }
    }
  }
  .right_box {
    width: 15%;
    line-height: 70px;
    i {
      color: #998d8d;
      font-size: 30px;
    }
  }
}
</style>
